package leetcode.code0904;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int totalFruit(int[] fruits) {
		int len = fruits.length, ans = 0;
		int[] hash = new int[len];
		for (int r = 0, l = 0, cnt = 0; r <= len; r++) {
			int kind = r < len ? fruits[r] : -1;
			if (kind == -1 || hash[kind]++ == 0) {
				if (kind == -1 || ++cnt == 3) {
					ans = Math.max(ans, r - l);
					while (cnt == 3 && l < len) {
						if (--hash[fruits[l++]] == 0) {
							cnt--;
						}
					}
				}
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(3, this.totalFruit(H.str2array3("[1,2,1]")));
		H.compare(3, this.totalFruit(H.str2array3("[0,1,2,2]")));
		H.compare(4, this.totalFruit(H.str2array3("[1,2,3,2,2]")));
		H.compare(5, this.totalFruit(H.str2array3("[3,3,3,1,2,1,1,2,3,3,4]")));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
